package com.example.zyfypt_206shj.fragement;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.zyfypt_206shj.R;
import com.example.zyfypt_206shj.adapter.ArticleAdapter;
import com.example.zyfypt_206shj.adapter.CollectionAdapter_article;
import com.example.zyfypt_206shj.adapter.CollectionAdapter_project;
import com.example.zyfypt_206shj.adapter.CollectionAdapter_tcase;
import com.example.zyfypt_206shj.adapter.CollectionAdapter_tware;
import com.example.zyfypt_206shj.adapter.CollectionAdapter_video;
import com.example.zyfypt_206shj.bean.ArticleBean;
import com.example.zyfypt_206shj.bean.CollectionBean;
import com.example.zyfypt_206shj.bean.ProjectBean;
import com.example.zyfypt_206shj.bean.SessionID;
import com.example.zyfypt_206shj.bean.TcaseBean;
import com.example.zyfypt_206shj.bean.TwareBean;
import com.example.zyfypt_206shj.bean.VideoBean;
import com.example.zyfypt_206shj.iface.ArticleListener;
import com.example.zyfypt_206shj.iface.CollectionListener;
import com.example.zyfypt_206shj.model.ArticleModel;
import com.example.zyfypt_206shj.model.CollectionModel_article;
import com.example.zyfypt_206shj.model.CollectionModel_project;
import com.example.zyfypt_206shj.model.CollectionModel_tcase;
import com.example.zyfypt_206shj.model.CollectionModel_tware;
import com.example.zyfypt_206shj.model.CollectionModel_video;

import java.util.List;

/**
 * Created by Neusoft on 2017/4/9.
 */

public class Fragment_collection extends Fragment {
    private String mod;
    private View view=null;
    private Context context;
    private RecyclerView recyclerView;
    private RecyclerView.LayoutManager layoutManager;
    private CollectionModel_article collectionModel_article;
    private CollectionModel_project collectionModel_project;
    private CollectionModel_tcase collectionModel_tcase;
    private CollectionModel_tware collectionModel_tware;
    private CollectionModel_video collectionModel_video;
    private CollectionAdapter_article collectionAdapter_article;
    private CollectionAdapter_project collectionAdapter_project;
    private CollectionAdapter_tcase collectionAdapter_tcase;
    private CollectionAdapter_tware collectionAdapter_tware;
    private CollectionAdapter_video collectionAdapter_video;
    private List<CollectionBean<ArticleBean>> list_article=null;
    private List<CollectionBean<ProjectBean>> list_project=null;//保存数据
    private List<CollectionBean<TcaseBean>> list_tcase=null;//保存数据
    private List<CollectionBean<TwareBean>> list_tware=null;//保存数据
    private List<CollectionBean<VideoBean>> list_video=null;//保存数据
    CollectionListener<ArticleBean> collectionListener_article=new CollectionListener<ArticleBean>() {
        @Override
        public void onResponse(List<CollectionBean<ArticleBean>> beanlist) {
            collectionAdapter_article.setList(beanlist);
            collectionAdapter_article.notifyDataSetChanged();
            Toast.makeText(context, "items:" + collectionAdapter_article.getItemCount(), Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(context, "fail" + msg, Toast.LENGTH_SHORT).show();
        }

    };

    CollectionListener<TcaseBean> collectionListener_tcase=new CollectionListener<TcaseBean>() {
        @Override
        public void onResponse(List<CollectionBean<TcaseBean>> beanlist) {
            collectionAdapter_tcase.setList(beanlist);
            collectionAdapter_tcase.notifyDataSetChanged();
            Toast.makeText(context, "items:" + collectionAdapter_tcase.getItemCount(), Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(context, "fail" + msg, Toast.LENGTH_SHORT).show();
        }

    };

    CollectionListener<TwareBean> collectionListener_tware=new CollectionListener<TwareBean>() {
        @Override
        public void onResponse(List<CollectionBean<TwareBean>> beanlist) {
            collectionAdapter_tware.setList(beanlist);
            collectionAdapter_tware.notifyDataSetChanged();
            Toast.makeText(context, "items:" + collectionAdapter_tware.getItemCount(), Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(context, "fail" + msg, Toast.LENGTH_SHORT).show();
        }

    };

    CollectionListener<VideoBean> collectionListener_video=new CollectionListener<VideoBean>() {
        @Override
        public void onResponse(List<CollectionBean<VideoBean>> beanlist) {
            collectionAdapter_video.setList(beanlist);
            collectionAdapter_video.notifyDataSetChanged();
            Toast.makeText(context, "items:" + collectionAdapter_video.getItemCount(), Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(context, "fail" + msg, Toast.LENGTH_SHORT).show();
        }

    };

    CollectionListener<ProjectBean> collectionListener_project=new CollectionListener<ProjectBean>() {
        @Override
        public void onResponse(List<CollectionBean<ProjectBean>> beanlist) {
            collectionAdapter_project.setList(beanlist);
            collectionAdapter_project.notifyDataSetChanged();
            Toast.makeText(context, "items:" + collectionAdapter_project.getItemCount(), Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(context, "fail" + msg, Toast.LENGTH_SHORT).show();
        }

    };

    public Fragment_collection(String mod) {
        this.mod = mod;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        context=getActivity();
        //动态加载Fragment1的布局文件
        view=inflater.inflate(R.layout.fragment_collection,container,false);
        //获取布局上的控件，注意通过view获取
//        tvinfo=(TextView)view.findViewById(R.id.textView8);
//        tvinfo.setText("微信");
        //返回动态生成的view
        initRecyclerView();

        if(mod.equals("article")){
            collectionModel_article= new CollectionModel_article();
            collectionModel_article.getResultList("article",1,SessionID.sessionID,collectionListener_article);
        }else if(mod.equals("tcase")){
            collectionModel_tcase= new CollectionModel_tcase();
            collectionModel_tcase.getResultList("tcase",1,SessionID.sessionID,collectionListener_tcase);
        }else if(mod.equals("tware")){
            collectionModel_tware= new CollectionModel_tware();
            collectionModel_tware.getResultList("tware",1,SessionID.sessionID,collectionListener_tware);
        }else if(mod.equals("video")){
            collectionModel_video= new CollectionModel_video();
            collectionModel_video.getResultList("video",1,SessionID.sessionID,collectionListener_video);
        }else if(mod.equals("project")){
            collectionModel_project= new CollectionModel_project();
            collectionModel_project.getResultList("project",1,SessionID.sessionID,collectionListener_project);
        }

        return view;
    }
    private void initRecyclerView() {

        layoutManager = new LinearLayoutManager(getContext());

        recyclerView=(RecyclerView)view.findViewById(R.id.recyclerView_collection);
        //创建默认线性布局
        recyclerView.setLayoutManager(layoutManager);
        //固定每个item的高度，可以提高性能
        recyclerView.setHasFixedSize(true);
        //实例化adapter

        if(mod.equals("article")){
            collectionAdapter_article=new CollectionAdapter_article(getContext());
            recyclerView.setAdapter(collectionAdapter_article);
        }else if(mod.equals("tcase")){
            collectionAdapter_tcase= new CollectionAdapter_tcase(getContext());
            recyclerView.setAdapter(collectionAdapter_tcase);
        }else if(mod.equals("tware")){
            collectionAdapter_tware= new CollectionAdapter_tware(getContext());
            recyclerView.setAdapter(collectionAdapter_tware);
        }else if(mod.equals("video")){
            collectionAdapter_video= new CollectionAdapter_video(getContext());
            recyclerView.setAdapter(collectionAdapter_video);
        }else if(mod.equals("project")){
            collectionAdapter_project= new CollectionAdapter_project(getContext());
            recyclerView.setAdapter(collectionAdapter_project);
        }
    }
}